Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify File.WriteAllTextAsync #55644

Merged
merged 1 commit into from
Jul 15, 2021
Merged

Conversation

stephentoub
Copy link
Member

Avoid the unnecessary ArrayPool usage and extra layer of buffering. The code ends up being much simpler, and also a bit faster.

Method Toolchain Mean Ratio Allocated
WriteAllTextAsync \main\CoreRun.exe 1.560 ms 1.00 22 KB
WriteAllTextAsync \pr\CoreRun.exe 1.486 ms 0.97 19 KB
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

[MemoryDiagnoser]
public class Program
{
    public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    private static string s_contents = string.Concat(Enumerable.Range(0, 100_000).Select(i => (char)('a' + (i % 26))));
    private static string s_path = Path.GetRandomFileName();
    private static CancellationTokenSource s_cts = new();

    [Benchmark]
    public Task WriteAllTextAsync() => File.WriteAllTextAsync(s_path, s_contents, s_cts.Token);
}

Avoid the unnecessary ArrayPool usage and extra layer of buffering.
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@stephentoub stephentoub added this to the 6.0.0 milestone Jul 14, 2021
@ghost
Copy link

ghost commented Jul 14, 2021

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

Avoid the unnecessary ArrayPool usage and extra layer of buffering. The code ends up being much simpler, and also a bit faster.

Method Toolchain Mean Ratio Allocated
WriteAllTextAsync \main\CoreRun.exe 1.560 ms 1.00 22 KB
WriteAllTextAsync \pr\CoreRun.exe 1.486 ms 0.97 19 KB
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

[MemoryDiagnoser]
public class Program
{
    public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

    private static string s_contents = string.Concat(Enumerable.Range(0, 100_000).Select(i => (char)('a' + (i % 26))));
    private static string s_path = Path.GetRandomFileName();
    private static CancellationTokenSource s_cts = new();

    [Benchmark]
    public Task WriteAllTextAsync() => File.WriteAllTextAsync(s_path, s_contents, s_cts.Token);
}
Author: stephentoub
Assignees: -
Labels:

area-System.IO

Milestone: -

@stephentoub stephentoub merged commit 9717cf5 into dotnet:main Jul 15, 2021
@stephentoub stephentoub deleted the writealltextasync branch July 15, 2021 02:41
@ghost ghost locked as resolved and limited conversation to collaborators Aug 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant